/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.components;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.filehandlers.CBVMap;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world.Chessboard;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world.Map;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world.TextureInfoBean;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * @author Massimo Canonaco
 *
 */
public class ImageProvider 
{
	/** */
	static private final String AVATAR_NAME = "avatar.png";
	
	/** */
	private String characterRoleFolder;
	
	/** */
	private String textureFolder;
	
	/** */
	private HashMap<Integer, Image> mapTextures;
	
	/** */
//	private HashMap<String, Image> avatars;
	
	private HashMap<String, ImageIcon> avatars;
	
	/** */
	public ImageProvider(final String characterFolder)
	{
		characterRoleFolder = characterFolder;
	}
	
	/** */
	public ImageProvider()
	{
	}
	
	
	/** */
	private void loadAvatar(final String role)
	{
		if (avatars == null)
		{
			avatars = new HashMap<>();
		}
		
		if(!avatars.containsKey(role))
		{
			avatars.put(role, new ImageIcon(characterRoleFolder+role+File.separator+AVATAR_NAME));
		}
	}
	
	/** */
	public ImageIcon getAvatar(final String role)
	{
		this.loadAvatar(role);
//		System.out.println(avatars.get(role).getIconHeight());
//		System.out.println(avatars.get(role).getIconWidth());
		if(avatars.get(role) == null)
			System.out.println("fuck");
		else
			System.out.println("tutto ok");
		return avatars.get(role);
	}
	
	/** */
	public Image getTexture(final TextureInfoBean textureBean)
	{
//		if(mapTextures == null)
//		{
//			mapTextures = new HashMap<>();
//		}
		
		int textureKey = textureBean.getAcutalKey();
		if(!mapTextures.containsKey(textureKey))
		{
			loadTexture(textureBean);
		}
		
		return mapTextures.get(textureKey);
	}
	
	/** */
	public void loadTextures(final Collection<TextureInfoBean> textureBeans)
	{
//		if(mapTextures == null)
//		{
//			mapTextures = new HashMap<>();
//		}
//		System.out.println(textureBeans.size());
//		for (TextureInfoBean texture : textureBeans) 
//		{
//			loadTexture(texture);
//		}
		
		
	}
	/** */
	public void loadTexture(final TextureInfoBean textureBean)
	{
		
		if(mapTextures == null)
		{
			mapTextures = new HashMap<>();
		}
		try 
		{
//			System.out.println(textureFolder+File.separatorChar+textureBean.getFolder()+File.separatorChar+textureBean.getName()+"."+textureBean.getExtension());
			mapTextures.put(textureBean.getAcutalKey(), ImageIO.read(new File(textureFolder+File.separatorChar+textureBean.getFolder()+File.separatorChar+textureBean.getName()+"."+textureBean.getExtension())));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		TOOLKIT.getImage(TEXTURE_FOLDER+File.separatorChar+textureBean.getFolder()+File.separatorChar+textureBean.getName());
	}
	
	public static void main(String[] args) 
	{
		TextureInfoBean neutral = new TextureInfoBean("first", "neutral", "jpg", 0);
		TextureInfoBean ywalk = new TextureInfoBean("first", "ywalk", "jpg", 1);
		TextureInfoBean nwalk = new TextureInfoBean("first", "nwalk", "jpg", 2);
		
		Chessboard arena = new Chessboard(0, 2);
		arena.setWalkable(0, 0, true);
		arena.setWalkable(0, 1, false);
		arena.setWalkable(1, 1, true);
		arena.setWalkable(1, 0, false);
		arena.setTexture(0, 0, neutral);
		arena.setTexture(0, 1, nwalk);
		arena.setTexture(1, 1, ywalk);
		arena.setTexture(1, 0, nwalk);
		arena.addSpawnCells(1, 1);
		
		Map map = new Map();
		map.setArena(arena);
		map.setName("first");
		map.setSalt("0125");
		map.setTextureToField(0, 0, neutral, true);
		map.setTextureToField(0, 1, nwalk, true);
		map.setTextureToField(1, 1, ywalk, true);
		map.setTextureToField(1, 0, nwalk, true);
//		
		CBVMap cbv = new CBVMap();
//		try {
//			Map map = cbv.build("cbvapps"+File.separator+"maps", "first");
//			ImageProvider i = new ImageProvider();
//			i.TEXTURE_FOLDER = "cbvapps"+File.separator+"textures";
//			i.getTexture(map.getTextures().get(0));
//			System.out.println("ciao");
//		} catch (IOException | InvalidFileException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		cbv.save("cbvapps"+File.separator+"maps", "first", map);
		
		
	}

	/**
	 * @return the characterRoleFolder
	 */
	public String getCharacterRoleFolder() {
		return characterRoleFolder;
	}

	/**
	 * @param characterRoleFolder the characterRoleFolder to set
	 */
	public void setCharacterRoleFolder(String characterRoleFolder) {
		this.characterRoleFolder = characterRoleFolder;
	}

	/**
	 * @return the textureFolder
	 */
	public String getTextureFolder() {
		return textureFolder;
	}

	/**
	 * @param textureFolder the textureFolder to set
	 */
	public void setTextureFolder(String textureFolder) {
		this.textureFolder = textureFolder;
	}
}
